package com.lanou3g.code0426;

import javax.lang.model.element.NestingKind;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Created by dllo on 18/4/26.
 */
public class Main {
    /*
    JDBC 一共有4个核心类
    1.DriverManager
    用来管理驱动的
    2.Connection
    3.Statement
    4.ResultSet
     */
    public static void main(String[] args) {
        //1.装载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 2.获得连接
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lan_ou",
                    "root",
                    "123456");
            // 3.获得执行SQL语句的对象
            Statement state = conn.createStatement();
            //4.执行sql语句
            String sql = "select * from stu;";
            ResultSet resultSet = state.executeQuery(sql);
            //  用来执行没有结果的SQL
            // 返回值 int 用来表示这条 sql影响了多少行数据
            //int row state.executeUpdate(sql);


            while (resultSet.next()) {
                String id = resultSet.getString("sid");
                String name = resultSet.getString("sname");
                String gender = resultSet.getString("gender");
                int age = resultSet.getInt("age");
                /*
                System.out.println(resultSet.getObject("sid"));
                System.out.println(resultSet.getObject("sname"));
                System.out.println(resultSet.getObject("gender"));
                System.out.println(resultSet.getObject("age"));
                 */
            }
            // 5. 关闭资源
            conn.close();
            state.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

